package crawlerGUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import scheduler.CrawlerScheduler;
import scheduler.ILinkRepository;
import scheduler.MySQLLinkRepository;
import server.CrawlerServer;
import structures.InfoPacket;
import structures.TaskType;
import crawler.CrawlerClient;

public class CrawlerMainWindow extends JFrame {

	
	private static final long serialVersionUID = 1L;
	
	//panel z zakłąddkami
	private JTabbedPane tabbedPane;
	//zakłądka SearchWord
	private JPanel panel1;
	//guzik SEARCH
	private JButton button1;
	//guzik Search links
	private JButton button2;
	//druga zakłądka
	private JPanel panel2;
	//pole w pierwszej zakłądce
	private JTextArea textArea1;
	//pole w drugiej zakładce
	private JTextArea textArea2;
	
	//serwer crawlera
	private CrawlerServer srv;
	//private JPanel clientPanel;
	//pole w pierwszej zakładce
	private JTextField textField11;
	//pole po prawej od zakłądek z aktualnymi klientami
	private JTextArea textAreaMainRight;
	
	//startuje serwer lub ustawia nowe zadanie
	public void startServer(InfoPacket pck) {
		
		LinkedList<String> l = new LinkedList<String>();
		l.add("http://simple.wikipedia.org/wiki/Main_Page");
		//ILinkRepository rep = new LinkRepository();
		ILinkRepository rep = new MySQLLinkRepository("jdbc:mysql://localhost/netcrawler", "tomek", "wereti");
		rep.init(l);
		
		if(srv==null){ //sprawdzamy czy już jest server odpowiadajacy temu okienku, ew. jeszcze pobieramy port
			srv = new CrawlerServer(5775, new CrawlerScheduler(rep), rep,this,pck);
			new CrawlerClient(5775,"127.0.0.1",2).start();
			Thread t = new Thread(srv);
			t.start();
			}
		else srv.updatePacket(pck);

	}
	
	//main ----
	public static void main(String[] args) {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
            	CrawlerMainWindow window = new CrawlerMainWindow();
        		window.createGUI();
        	}
		});
	}
	
	//tworzenie głównego okna
	public void createGUI() {

		this.setName("Crawler Command Center");
		this.setSize(900, 500);

		Container content = this.getContentPane();
		content.setBackground(Color.LIGHT_GRAY);
		
		//tworzenie tabów
		CreateStringSearchWindow();		
		CreateURLSearchWindow();
		
		//panel główny
		JPanel topPanel = new JPanel();
		topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.X_AXIS));
		content.add(topPanel);
		
		//panel po lewej
		this.tabbedPane = new JTabbedPane();		
		this.tabbedPane.addTab("WordSearch", panel1);
		this.tabbedPane.addTab("URLSearch", panel2);
		
		
		//panel po prawej
		textAreaMainRight=new JTextArea();
		textAreaMainRight.setLineWrap(true);
		textAreaMainRight.setWrapStyleWord(true);
		textAreaMainRight.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 4));
		textAreaMainRight.setEditable(false);
		
		JPanel eastPanel=new JPanel();
		eastPanel.setLayout(new BoxLayout(eastPanel, BoxLayout.Y_AXIS));
		JScrollPane pane=new JScrollPane();
        pane.getViewport().add(textAreaMainRight);
        eastPanel.add(pane);
        
        
        //dodanie obu głównych paneli
        topPanel.add(tabbedPane);
        topPanel.add(eastPanel);
        
        
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setVisible(true);
	}

	//tworzenie zakładki szukającej słów kluczowych
	public void CreateStringSearchWindow(){
		//szukanie stringow
		panel1=new JPanel();
		panel1.setLayout(new BorderLayout());
		panel1.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
		
		textArea1=new JTextArea();
		JScrollPane pane = new JScrollPane();

		textArea1.setLineWrap(true);
		textArea1.setWrapStyleWord(true);
		textArea1.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
		textArea1.setEditable(false);
		
        pane.getViewport().add(textArea1);
        panel1.add(pane,BorderLayout.CENTER);
        
        textField11=new JTextField();
        textField11.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1) );
        textField11.addActionListener(new ActionListener(){
        	 public void actionPerformed(ActionEvent event) {
        		 startWordSearch();
                 
                
             }
        });
        JPanel tempPanel=new JPanel();
        tempPanel.setLayout(new BorderLayout());
        
		button1=new JButton("SEARCH");
		button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
            	startWordSearch();
                
               
            }
        });

		JButton button3=new JButton("STOP");
		button3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
            	new Thread(new Runnable(){

					@Override
					public void run() {
						srv.stop();
					}}).start();
            		//srv = null;
               
            }
        });
		
		tempPanel.add(button1,BorderLayout.EAST);
		tempPanel.add(button3,BorderLayout.WEST);
		tempPanel.add(textField11,BorderLayout.CENTER);
		JLabel label=new JLabel("Strony zawierające podane słowo kluczowe:\n");
		panel1.add(label,BorderLayout.NORTH);
		panel1.add(tempPanel,BorderLayout.SOUTH);
	}
	
	public void startWordSearch(){
		textArea1.setText("");
    	textArea2.setText("");
    	InfoPacket pck=new InfoPacket();
    	pck.addTask(TaskType.keyword, textField11.getText());
        startServer(pck);
	}
	
	//tworzenie zakładki z linkami
	public void CreateURLSearchWindow(){
		//szukanie stringow
		panel2=new JPanel();
		panel2.setLayout(new BorderLayout());
		panel2.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
		
		textArea2=new JTextArea();
		JScrollPane pane = new JScrollPane();

		textArea2.setLineWrap(true);
		textArea2.setWrapStyleWord(true);
		textArea2.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
		textArea2.setEditable(false);
        pane.getViewport().add(textArea2);
        panel2.add(pane);

		button2=new JButton("Search");
		button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
            	textArea1.setText("");
            	textArea2.setText("");
                startServer(new InfoPacket());
            }
        });
		JLabel label=new JLabel("Wyszukane linki:\n");
		panel2.add(label,BorderLayout.NORTH);
		panel2.add(button2,BorderLayout.SOUTH);
	}
	
	//dodawanie nowych linków
	public void updateURLs(List<String> URLs){
		for(String s:URLs){
		textArea2.append(s+"\n");}
	}
	
	//dodawanie stron z szukanym słowem
	public void updatePhrasesFound(String found){
		textArea1.append(found+"\n");
	}
	
	//dodawanie nowych klientów
	public void addClient(String name){
		this.textAreaMainRight.append("Client address = " +name+"\n");
	}
	
}